/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.siagefi.controller;

import br.siagefi.model.Representante;
import br.siagefi.service.RepresentanteService;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Paulo Lavarini
 */

@SessionScoped
@ManagedBean
public class RepresentanteBean implements Serializable{
    
    private Representante representante;
    private final RepresentanteService service;
    private List<Representante> representantes;

    public RepresentanteBean() {
        this.representante = new Representante();
        this.service = new RepresentanteService();
        this.carregarLista();
    }
    
     public final void carregarLista(){
         this.representantes = service.listar();
    }
    
    public String gravar(){
        service.gravar(this.getRepresentante());
        this.limparFormularioDoJsf();
        this.carregarLista();
        return "representante?faces-redirect=true";
    }
    
    public String alterar(Representante representante){
        this.setRepresentante(representante);
        return "incluirRepresentante";
    }
    
     public String remover(){
        service.remover(this.getRepresentante());
        this.limparFormularioDoJsf();
        this.carregarLista();
        return "representante";
    }
     
    public String remover(Representante representante){
        service.remover(representante);
        this.limparFormularioDoJsf();
        this.carregarLista();
        return "representante";
    }
  
    public void limparFormularioDoJsf(){
        this.representante = new Representante();
    }

    public Representante getRepresentante() {
        return representante;
    }

    public void setRepresentante(Representante representante) {
        this.representante = representante;
    }

    public List<Representante> getRepresentantes() {
        return representantes;
    }

    public void setRepresentantes(List<Representante> representantes) {
        this.representantes = representantes;
    }
}
